package zdream.pmfield.stage.buff.gain;

import java.util.Objects;

import zdream.pmfield.data.foe.MoveAdditionalEffects;
import zdream.pmfield.stage.buff.foe.FoeStageBuff;
import zdream.pmfield.stage.director.AttackContext;
import zdream.pmfield.stage.director.HookType;
import zdream.pmfield.stage.director.Hooks;
import zdream.pmfield.stage.message.FoeRemoveBuffMessage;
import zdream.pmfield.stage.object.FoeActor;
import zdream.pmfield.stage.object.StageMove;

/**
 * <p>热衷状态
 * <p>热衷状态会记录造成状态的 {@link StageMove}, 该技能造成的伤害提升 30%
 * 处于热衷状态的宝可梦防御降低 20%
 * <p>每次用技能都会删除热衷状态. 但重复使用造成热衷状态的技能会先删除状态再增加状态,
 * 所以看起来是状态没有变化.
 * </p>
 *
 * @author Zdream
 * @date 2023-12-18
 * @since 0.0.1
 */
public class FixatedBuff extends FoeStageBuff {
	public static final String CATEGORY = MoveAdditionalEffects.E_FIXATED;
	public final StageMove move;

	public FixatedBuff(FoeActor target) {
		super(target);
		move = Objects.requireNonNull(target.findMoveExecutingBuff().move);
	}

	@Override
	public void init() {
		onHook(HookType.ON_CHECK_TYPE_FIXED)
				.handle(this::increaseDamage)
				.buildAndSave();
		onHook(HookType.AFTER_EXECUTE_ATTACK)
				.handle(this::removeSelf)
				.withPriority(-800)
				.buildAndSave();
	}

	private void increaseDamage(Hooks.HookContext rawContext) {
		final Hooks.AttackHookContext context = rawContext.as();
		final AttackContext attackContext = context.attackContext;

		if (attackContext.hasAttacker(target)) {
			if (attackContext.currentMove == move) {
				attackContext.effectFixed *= 1.3f;
			}
			return;
		}

		final AttackContext.AttackContextItem item = attackContext.findAttackItemForDefender(target);
		if (item == null) {
			return;
		}
		item.defenseFixed *= 0.8f;
	}

	private void removeSelf(Hooks.HookContext rawContext) {
		Hooks.AttackHookContext context = rawContext.as();
		if (context.attackContext.hasAttacker(target)) {
			target.removeBuff(this);
			pushMessage(new FoeRemoveBuffMessage(this));
		}
	}

	@Override
	public String getCategory() {
		return CATEGORY;
	}

	@Override
	public String toString() {
		return "热衷:" + move.move.name;
	}
}
